Skip to content

Improve perfs - #89

Merged
garak merged 1 commit into
p-chess:masterfrom
garak:improve-perfs
Sep 1, 2026
Merged

garak merged 1 commit into
p-chess:masterfrom
garak:improve-perfs

Conversation

@garak

@garak garak commented Jul 10, 2026

Copy link
Copy Markdown
Member

Fix #88

@garak

garak commented Jul 10, 2026

Copy link
Copy Markdown
Member Author
Subject Old New Delta Improvement
benchMove 15.689 us 13.244 us -2.445 us 15.58% faster
benchMoves 14.473 us 13.461 us -1.012 us 6.99% faster

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses performance issue #88 by replacing the O(board size) json_encode($this->board)-based position hashing with an incrementally maintained Zobrist-style hash, so repeated makeMove() / undoMove() calls during move generation don’t re-serialize the entire board.

Changes:

  • Introduces ZobristHasher and integrates it into Chess to maintain boardHash in O(1) per move/undo.
  • Updates generateMoves() cache key construction to avoid ambiguity/collisions (adds delimiters + explicit "all" when $square is null).
  • Updates tests to assert deterministic hashing and that side-to-move affects the hash.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/Chess.php Swaps board hashing to incremental Zobrist updates on make/undo; refactors cache key format; adds refreshBoardHash().
src/ZobristHasher.php New hasher implementing rebuild/apply/restore for a 64-bit (8-byte) hash encoded as hex.
src/Entry.php Stores a previousPosition hash to enable O(1) hash restoration on undo.
tests/MiscTest.php Updates hash expectations to reflect deterministic Zobrist hashing and side-to-move inclusion.
.gitignore Updates ignore patterns (root *.cache, adds /build/).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Chess.php Resolved
Comment thread src/ZobristHasher.php Resolved
Comment thread src/Chess.php Resolved
Comment thread tests/MiscTest.php Resolved
@garak
garak force-pushed the improve-perfs branch 2 times, most recently from 86b3860 to 9236c62 Compare July 11, 2026 08:14
@garak
garak requested a review from akondas July 11, 2026 08:17
@garak
garak merged commit a19d943 into p-chess:master Sep 1, 2026
5 checks passed
@garak
garak deleted the improve-perfs branch September 1, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve performance

2 participants